package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //分页参数设置
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //查询未分页的额数据
        List<SetmealVO> setmealVOS = setmealMapper.pageQuery(setmealPageQueryDTO);

        Page page= (Page) setmealVOS;


        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        //根据id查询套餐
        SetmealVO setmealById = setmealMapper.getSetmealById(id);

        //根据id 查套餐关联的菜品
        List<SetmealDish> setmealDishList = setmealDishMapper.getBysetmealId(id);



        setmealById.setSetmealDishes(setmealDishList);




        return  setmealById;
    }


    @Transactional
    @Override
    public Result insertSetmeal(SetmealVO setmealVO) {
        //setmealvo没有创建人和创建时间
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        //插入套餐信息
        setmealMapper.insertSetmeal(setmeal);

        log.info("套餐信息："+setmeal);


        //关联菜品
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        log.info("套餐关联的菜品："+setmealDishes);
        List<Long> ids = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            log.info("菜品id"+setmealDish.getDishId());
            ids.add(setmealDish.getDishId());
        }
//        setmealDishes.forEach(setmealDish -> {
//            setmealDish.setSetmealId(setmeal.getId());
//            log.info("菜品id"+setmealDish.getDishId());
//            ids.add(setmealDish.getDishId());
//        });


        //查看菜品是否有禁售状态
        List<Dish> dishByStatus = dishMapper.getDishByStatusToO(ids);
        if(dishByStatus.size()>0){
            throw new BaseException(MessageConstant.DISH_NO_SALE);
            //Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
        }


        //为套餐添加关联的菜品
        setmealDishMapper.insertSetmealDishs(setmealDishes);
        //setmealMapper.insertSetmeeal(setmeal);
        return Result.success();
    }


    @Override
    public Result updateStatus(Integer status, Long id) {
        Setmeal setmeal=new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);

        //起售停售前查询是否有禁售的菜品
        //select *from dish where status=0 and id in(select  dish_id from setmeal_dish where setmeal_id=#{id})
        List<Dish> dishStatus = dishMapper.getDishStatus(id);

        //在售状态也不能删除
        List<Long> ids=new ArrayList<>();
        ids.add(id);
        //查询是否在售
        List<Setmeal> setmealByStatus = setmealMapper.getSetmealByStatus(ids);
        if(dishStatus.size()>0 && setmealByStatus.size()<1){
            return Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
        }


        setmealMapper.updateStatus(setmeal);
        return Result.success();

    }

    @Transactional
    @Override
    public Result deleteBatch(List<Long> ids) {
        //勾选的套餐中有在售状态
        List<Setmeal> setmealByStatus = setmealMapper.getSetmealByStatus(ids);
        if(setmealByStatus.size()>0){
            //在售状态下，不可删除套餐
            return Result.error(MessageConstant.SETMEAL_ON_SALE);
        }
        
        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBySetmealId(ids);
        return Result.success();
    }


    @Transactional
    @Override
    public void updateSetmeal(SetmealVO setmealVO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        //修改套餐信息
        setmealMapper.updateSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        //修改套餐和菜品的中间表
        //1、先将中间的套餐删除
        setmealDishMapper.deleteById(setmeal.getId());

        //再更新中间表
        setmealDishMapper.insertSetmealDishs(setmealDishes);


    }


    /**
     * 根据套餐id查询套餐中的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        return null;
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }





}
